import * as vscode from 'vscode';
import { AIService } from '../aiService';

export async function configureAI(aiService: AIService): Promise<void> {
    try {
        // Get current configuration
        const currentConfig = aiService.getConfig() || {
            baseUrl: 'https://api.openai.com',
            apiKey: '',
            model: 'gpt-3.5-turbo',
            systemPrompt: 'You are an excellent assistant. Please automatically adjust the language of your responses based on the user\'s question language. If the user asks in Chinese, please answer in Chinese; if the user asks in English, please answer in English.'
        };

        // Prompt for base URL
        const baseUrl = await vscode.window.showInputBox({
            prompt: 'Enter the base URL for the OpenAI-compatible API',
            value: currentConfig.baseUrl,
            placeHolder: 'https://api.openai.com'
        });
        
        if (baseUrl === undefined) {
            return; // User cancelled
        }

        // Prompt for API key
        const apiKey = await vscode.window.showInputBox({
            prompt: 'Enter your API key',
            value: currentConfig.apiKey,
            password: true
        });
        
        if (apiKey === undefined) {
            return; // User cancelled
        }

        // Prompt for model name
        const model = await vscode.window.showInputBox({
            prompt: 'Enter the model name',
            value: currentConfig.model,
            placeHolder: 'gpt-3.5-turbo'
        });
        
        if (model === undefined) {
            return; // User cancelled
        }

        // Prompt for system prompt
        const systemPrompt = await vscode.window.showInputBox({
            prompt: 'Enter the system prompt for the AI',
            value: currentConfig.systemPrompt || 'You are an excellent assistant. Please automatically adjust the language of your responses based on the user\'s question language. If the user asks in Chinese, please answer in Chinese; if the user asks in English, please answer in English.',
            placeHolder: 'System instructions for the AI model'
        });
        
        if (systemPrompt === undefined) {
            return; // User cancelled
        }

        // Save the configuration
        await aiService.saveConfig({
            baseUrl,
            apiKey,
            model,
            systemPrompt
        });

        vscode.window.showInformationMessage('AI configuration saved successfully');
    } catch (error) {
        vscode.window.showErrorMessage(`Error configuring AI: ${error}`);
    }
}
